﻿/*
 * @file: OrdersDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for OrdersDA
/// </summary>
namespace DAL
{
    public class OrdersDA : BaseDAL, IOrders
    {
        private const string tableName = "Orders";
        private const string orderID = "OrderID";
        private const string userID = "UserID";
        private const string totalAmount = "TotalAmount";
        private const string billDate = "BillDate";
        private const string orderDate = "OrderDate";
        private const string isEnable = "IsEnable";

        public OrdersDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IOrders Members

        /*
         * @method: GetOrders
         * @description: get all Orders records
         * @input: 
         * @output: all records corresponding
         */
        public Orders[] GetOrders(bool allowFilter)
        {
            Orders[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = 1", tableName, isEnable);
                else
                    cmd.CommandText = String.Format("SELECT * FROM {0}", tableName);

                String[] columnNames = {    orderID,
                                            userID,
                                            totalAmount,
                                            billDate,
                                            orderDate,
                                            isEnable};
                result = SelectCollection<Orders>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetOrdersByDate
         * @description: get all Orders by Date
         * @input: OrdersDate
         * @output: all records corresponding
         */
        public Orders[] GetOrdersByDate(string _ordersDate, bool _allowFilter)
        {
            Orders[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (_allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, orderDate, orderDate, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", orderDate), SqlDbType.Int).Value = _ordersDate;
                }
                else {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, orderDate, orderDate);
                    cmd.Parameters.Add(String.Format("@{0}", orderDate), SqlDbType.Int).Value = _ordersDate;
                }

                String[] columnNames = {    orderID,
                                            userID,
                                            totalAmount,
                                            billDate,
                                            orderDate,
                                            isEnable};
                result = SelectCollection<Orders>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: InsertOrder
         * @description: insert new Order to database
         * @input: Order entity
         * @output: 1 if insert successful, 0 if error occurred
         */
        public int InsertOrder(Orders _order, out int autoNumber)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    userID,
                                            totalAmount};

                Object[] values = { _order.UserID,
                                    _order.TotalAmount};
                
                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetOrdersByuserID
         * @description: get all Orders by CustomerID
         * @input: CustomerID
         * @output: all records corresponding
         */
        public Orders[] GetOrdersByUser(int _userID, bool _allowFilter)
        {
            Orders[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (_allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, userID, userID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", userID), SqlDbType.Int).Value = _userID;
                }
                else {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, userID, userID);
                    cmd.Parameters.Add(String.Format("@{0}", userID), SqlDbType.Int).Value = _userID;
                }


                String[] columnNames = {    orderID,
                                            userID,
                                            totalAmount,
                                            billDate,
                                            orderDate};
                result = SelectCollection<Orders>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }


        /*'
         * @method: GetOrdersByOrderID
         * @description: get Order by OrderID
         * @input: OrderID
         * @output: record corresponding
         */
        public Orders GetOrderByOrderID(int _orderID, bool _allowFilter)
        {
            Orders[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (_allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, orderID, orderID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", orderID), SqlDbType.Int).Value = _orderID;
                }
                else
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, orderID, orderID);
                    cmd.Parameters.Add(String.Format("@{0}", orderID), SqlDbType.Int).Value = _orderID;
                }


                String[] columnNames = {    orderID,
                                            userID,
                                            totalAmount,
                                            billDate,
                                            orderDate};
                result = SelectCollection<Orders>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        public int DisableOrder(int _orderID, bool _isEnable) {
            int result = 0;
            try
            {
                String[] columnNames = { isEnable };
                Object[] values = { _isEnable };
                String[] keyColumnNames = { orderID };
                Object[] keyColumnValues = { _orderID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}